package com.tensquare.spit.controller;
/*
 *ClassName:SpitController
 *Description:吐槽的控制层
 *@author:可爱的大鸭子
 */

import com.tensquare.spit.pojo.Spit;
import com.tensquare.spit.service.SpitService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/spit")
public class SpitController {
    @Autowired
    private SpitService spitService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private HttpServletRequest request;

    /**
     * 查询全部数据
     * @return
     */
    @RequestMapping(method= RequestMethod.GET)
    public Result findAll(){
        return new Result(true, StatusCode.OK,"查询成功",spitService.findAll());
    }
    /**
     * 根据ID查询
     * @param id ID
     * @return
     */
    @RequestMapping(value="/{id}",method=RequestMethod.GET)
    public Result findOne(@PathVariable String id){
        System.out.println("id:"+id);
        return new Result(true,StatusCode.OK,"查询成功",spitService.findById(id));
    }

    /**
     * 增加:发表吐槽
     * @param spit
     */
    @RequestMapping(method=RequestMethod.POST)
    public Result add(@RequestBody Spit spit ){
        //验证用户是否登录
        Claims claims=(Claims)request.getAttribute("user_claims");
        if(claims==null) return new Result(false,StatusCode.ACCESSERROR,"请先登录哦~");
        String id = claims.getId();
        spit.set_id(id);
        String nickname = claims.getSubject();
        spit.setNickname(nickname);
        spitService.add(spit);
        return new Result(true,StatusCode.OK,"增加成功");
    }

    /**
     * 修改吐槽
     * @param spit
     * @param id
     * @return
     */
    @RequestMapping(value="/{id}",method=RequestMethod.PUT)
    public Result update(@RequestBody Spit spit,@PathVariable String id )
    {
        //验证用户是否登录
        Claims claims=(Claims)request.getAttribute("user_claims");
        if(claims==null) return new Result(false,StatusCode.ACCESSERROR,"请先登录哦~");
        spit.set_id(id);
        spitService.update(spit);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /**
     * 删除吐槽
     * @param id
     */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public Result deleteById(@PathVariable String id ){
        //验证用户是否登录
        Claims claims=(Claims)request.getAttribute("user_claims");
        if(claims==null) return new Result(false,StatusCode.ACCESSERROR,"请先登录哦~");
        spitService.deleteById(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /**
     * 根据上级ID查询吐槽分页数据
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value="/comment/{parentId}/{page}/{size}",method=RequestMethod.GET)
    public Result findByParentid(@PathVariable String parentId,@PathVariable int page,@PathVariable int size){
        Page<Spit> pageList = spitService.findByParentid(parentId,page, size);
        return new Result(true,StatusCode.OK,"查询成功",new
                PageResult<Spit>(pageList.getTotalElements(), pageList.getContent() ) );
    }

    /**
     * 点赞
     * @param id 吐槽的id
     * @return
     */
    @RequestMapping(value="/thumbup/{id}",method=RequestMethod.PUT)
    public Result updateThumbup(@PathVariable String id){
        //查询出登录用户的id
        Claims claims=(Claims)request.getAttribute("user_claims");
        if(claims==null) return new Result(false,StatusCode.ACCESSERROR,"请先登录");
        String userid = claims.getId();
        //判断用户是否点过赞
        if(redisTemplate.opsForValue().get("thumbup_"+userid+"_"+id)!=null){
            return new Result(false,StatusCode.REPERROR,"你已经点过赞了");
        }
        spitService.updateThumbup(id);
        redisTemplate.opsForValue().set( "thumbup_"+userid+"_"+ id,"1");
        return new Result(true,StatusCode.OK,"点赞成功");
    }
    /**
     * 浏览量，不登录浏览量也可以增加
     * @param id
     * @return
     */
    @RequestMapping(value = "/visits/{id}",method = RequestMethod.PUT)
    public Result updateVisits(@PathVariable String id){
        //判断用户是否点过赞已经浏览过
        String userid="1";
        if(redisTemplate.opsForValue().get("visits_"+userid+"_"+id)!=null){
            return new Result(false,StatusCode.REPERROR,"你已经浏览过了");
        }
        spitService.updateVisits(id);
        redisTemplate.opsForValue().set( "visits_"+userid+"_"+ id,"1");
        return new Result(true,StatusCode.OK,"浏览成功");
    }
    /**
     * 分享数，不登录也可以分享
     * @param id
     * @return
     */
    @RequestMapping(value = "/share/{id}",method = RequestMethod.PUT)
    public Result updateShare(@PathVariable String id){
        //判断用户是否已经转发过
        String userid="1";
        if(redisTemplate.opsForValue().get("share_"+userid+"_"+id)!=null){
            return new Result(false,StatusCode.REPERROR,"你已经分享过了");
        }
        spitService.updateVisits(id);
        redisTemplate.opsForValue().set( "share_"+userid+"_"+ id,"1");
        return new Result(true,StatusCode.OK,"分享成功");
    }

    /**
     * 根据条件查询Spit列表
     * @param spit
     * @return
     */
    @RequestMapping(value = "/search",method = RequestMethod.POST)
    public Result findSearch(@RequestBody Spit spit){
        return new Result(true,StatusCode.OK,"查询成功",
                spitService.findSearch(spit));
    }

    //查询全部吐槽
    @RequestMapping(value="/search/{page}/{size}",method=RequestMethod.POST)
    public Result findSearch(@RequestBody Spit spit , @PathVariable int page, @PathVariable int size){
        Page<Spit> pageList = spitService.findSearch(spit, page, size);
        return  new Result(true,StatusCode.OK,"查询成功",  new PageResult<Spit>(pageList.getTotalElements(), pageList.getContent()) );
    }

    //根据吐槽id查询所有评论
    @RequestMapping(value="/commentlist/{parentId}",method=RequestMethod.GET)
    public Result findByParentid(@PathVariable String parentId){
        List<Spit> list= spitService.findByParentid(parentId);
        return new Result(true,StatusCode.OK,"查询成功",list);
    }

    //查询热门吐槽
    @RequestMapping(value = "/hotSpits",method=RequestMethod.GET)
    public Result getHotSpits(){
        List<Spit> list= spitService.getHotSpits();
        return new Result(true,StatusCode.OK,"查询成功",list);
    }
}
